package com.ruoyi.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(tags = "微信支付相关")
@RestController
@RequestMapping("/wx/pay")
public class WechatPayController {

    @Resource
    private WxPayService wxPayService;

    @ApiOperation("统一下单")
    @PostMapping("/unifiedOrder")
    public AjaxResult unifiedOrder(@ApiParam("商品描述") @RequestParam String body, @ApiParam("商户订单号") @RequestParam String outTradeNo, @ApiParam("总金额(分)") @RequestParam Integer totalFee, @ApiParam("用户标识") @RequestParam String openid) {
        try {
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setBody(body);
            orderRequest.setOutTradeNo(outTradeNo);
            orderRequest.setTotalFee(totalFee);
            orderRequest.setOpenid(openid);
            orderRequest.setTradeType("JSAPI");

            WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
            return AjaxResult.success(null, orderResult);
        } catch (WxPayException e) {
            return AjaxResult.error("统一下单失败: " + e.getMessage());
        }
    }

    @ApiOperation("查询订单")
    @PostMapping("/queryOrder")
    public AjaxResult queryOrder(@ApiParam("商户订单号") @RequestParam String outTradeNo) {
        try {
            WxPayOrderQueryRequest queryRequest = new WxPayOrderQueryRequest();
            queryRequest.setOutTradeNo(outTradeNo);

            WxPayOrderQueryResult queryResult = wxPayService.queryOrder(queryRequest);
            return AjaxResult.success(null, queryResult);
        } catch (WxPayException e) {
            return AjaxResult.error("查询订单失败: " + e.getMessage());
        }
    }

    @ApiOperation("关闭订单")
    @PostMapping("/closeOrder")
    public AjaxResult closeOrder(@ApiParam("商户订单号") @RequestParam String outTradeNo) {
        try {
            WxPayOrderCloseResult closeResult = wxPayService.closeOrder(outTradeNo);
            return AjaxResult.success(null, closeResult);
        } catch (WxPayException e) {
            log.error("关闭订单失败", e);
            return AjaxResult.error("关闭订单失败: " + e.getMessage());
        }
    }

    @ApiOperation("退款操作")
    @PostMapping("/refund")
    public AjaxResult refund(@ApiParam("商户订单号") @RequestParam String outTradeNo, @ApiParam("商户退款单号") @RequestParam String outRefundNo, @ApiParam("订单总金额(分)") @RequestParam Integer totalFee, @ApiParam("退款金额(分)") @RequestParam Integer refundFee) {
        try {
            WxPayRefundRequest refundRequest = new WxPayRefundRequest();
            refundRequest.setOutTradeNo(outTradeNo);
            refundRequest.setOutRefundNo(outRefundNo);
            refundRequest.setTotalFee(totalFee);
            refundRequest.setRefundFee(refundFee);

            WxPayRefundResult refundResult = wxPayService.refund(refundRequest);
            return AjaxResult.success(null, refundResult);
        } catch (WxPayException e) {
            log.error("退款失败", e);
            return AjaxResult.error("退款失败: " + e.getMessage());
        }
    }

    @PostMapping("/notify")
    public String notify(@RequestBody String xmlData) {
        try {
            WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
            // 在这里处理支付回调逻辑、比如更新订单状态、记录日志等
            log.info("支付回调通知: {}", notifyResult);
            // 构造成功响应的XML
            String successXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
            return successXml;
        } catch (WxPayException e) {
            String failXml = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[支付回调处理失败]]></return_msg></xml>";
            return failXml;
        }
    }
}
